<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <link href="/URMS/lib/layui-v2.5.5/css/layui.css" media="all" rel="stylesheet">
    <link href="/URMS/css/public.css" media="all" rel="stylesheet">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form action="" class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input autocomplete="off" class="layui-input" name="username" type="text">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">账号</label>
                            <div class="layui-input-inline">
                                <input autocomplete="off" class="layui-input" name="sex" type="text">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户职业</label>
                            <div class="layui-input-inline">
                                <input autocomplete="off" class="layui-input" name="classify" type="text">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-primary" lay-filter="data-search-btn" lay-submit
                                    type="submit"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


        <script id="toolbarDemo" type="text/html">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <script id="currentTableBar" type="text/html">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="look">查看</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="add">添加权限</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script charset="utf-8" src="/URMS/lib/layui-v2.5.5/layui.js"></script>
<script>
    layui.use(['form', 'table'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/URMS/sysRole/selectRoleList',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cellMinWidth: 150,
            limits: [10, 15, 20],
            limit: 10,
            page: true,
            skin: 'line',
            cols: [
                [
                    {type: "checkbox", field: 'left'},
                    {field: 'id', title: 'ID', width: 150, sort: true},
                    {field: 'name', title: '角色名', width: 150, sort: true},
                    {
                        field: 'status', width: 150, title: '状态', align: 'center', sort: true,
                        templet: function (data) {
                            let res;

                            if (data.status == 1) {
                                res = "<div><a class=\"layui-btn layui-btn-normal layui-btn-xs \" name='status'>可用</a></div>"
                            } else {
                                res = "<div><a class=\"layui-btn layui-btn-xs layui-btn-danger layui-btn-disabled\" name='status'>禁用</a></div>"
                            }
                            return res;
                        }
                    },
                    {
                        field: 'createTime',
                        width: 250,
                        title: '创建时间',
                        align: 'center',
                        templet: function (data) {
                            return "<div id='createtime' name='createtime'>" + data.createdate + "</div>"
                        }
                    },
                    {
                        field: 'modifyTime',
                        width: 250,
                        title: '更改时间',
                        align: 'center',
                        templet: function (data) {
                            return "<div id='modifytime' name='modifytime'>" + data.modifydate != null ? data.modifydate : '' + "</div>"
                        }
                    },
                    {fixed: 'right', title: '操作', align: 'center', toolbar: '#currentTableBar'}
                ]
            ]
        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });


        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加角色',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/URMS/sysRole/add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            switch (layEvent) {
                case "look":
                    var index = layer.open({
                        title: '查看角色',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/URMS/sysUser/look?id=' + data.id,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    break;
                case "add":
                    var index = layer.open({
                        title: '添加角色权限',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/URMS/sysRole/addPermissions?id=' + data.id,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    table.reload();
                    break;
                case "edit":
                    var index = layer.open({
                        title: '编辑角色',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/URMS/sysRole/edit?id=' + data.id,
                    });
                    $(window).on("resize", function () {
                        table.reload();
                        layer.full(index);
                    });
                    break;
                case "delete":
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: '/URMS/sysRole/deleteRole',
                            type: 'post',
                            dataType: 'json',
                            data: {id: data.id},
                            success: function (res) {
                                if (res == true) {
                                    obj.del();
                                    layer.confirm("删除成功！")
                                } else {
                                    layer.confirm("删除失败！")
                                }
                                table.reload();
                            },
                            error: function () {
                                layer.confirm("删除出错了！")
                            }
                        })

                        layer.close(index);
                    });
                    break;
            }
            return false;
        })
    })
</script>

</body>
</html>